package com.amigo.online.provider.manager.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.amigo.online.common.base.service.BaseService;
import com.amigo.online.common.param.RedisKey;
import com.amigo.online.common.param.result.PhotoInfo;
import com.amigo.online.common.param.result.UserInfo;
import com.amigo.online.provider.manager.mapper.UserPhotosMapper;
import com.amigo.online.provider.manager.model.entity.UserPhotosTbl;
import com.amigo.online.provider.manager.mongo.dao.UserDao;
import com.amigo.online.provider.manager.mongo.entity.UserEntity;
import com.amigo.online.provider.manager.service.RedisService;
import com.amigo.online.provider.manager.service.UserPhotosService;

@Service
public class UserPhotosServiceImpl extends BaseService<UserPhotosTbl> implements UserPhotosService {
    private static final Logger logger = Logger.getLogger(UserPhotosServiceImpl.class);

    private static String user_key = RedisKey.USER_REDIS_KEY;
    @Resource
    private HashOperations<String, Long, Object> hashOperations;

    @Resource
    private RedisService redisService;

    @Resource
    private UserPhotosMapper userPhotosMapper;

    @Autowired
    private UserDao userDao;

    @Resource
    private RedisTemplate<String, UserInfo> redisTemplate;

    @Override
    public List<UserPhotosTbl> findUserPhotosTblList(UserPhotosTbl userPhotosTbl) {
        return userPhotosMapper.findUserPhotosTblList(userPhotosTbl);
    }

    @Override
    public boolean updateUserPhotos(UserPhotosTbl userPhotosTbl) {

        if (userPhotosMapper.updateUserPhotos(userPhotosTbl) <= 0) {
            return false;
        } else {
            logger.info("redis===============" + hashOperations.hasKey(user_key, userPhotosTbl.getUserId()));
            if (hashOperations.hasKey(user_key, userPhotosTbl.getUserId())) {
                UserInfo users = (UserInfo) hashOperations.get(user_key, userPhotosTbl.getUserId());

                logger.info("redis===============" + users);

                List<PhotoInfo> photos = users.getPhotos();

                for (PhotoInfo photoInfo : photos) {

                    if (photoInfo.getId() == userPhotosTbl.getId()) {
                        photoInfo.setCheck(userPhotosTbl.getChecked());
                    }
                }
                users.setPhotos(photos);
                hashOperations.put(user_key, userPhotosTbl.getUserId(), users);

                if (userPhotosTbl.getFirstImg() == 1) {
                    UserEntity userById = userDao.getUserById(userPhotosTbl.getUserId());
                    logger.info("Checked====" + userPhotosTbl.getChecked());
                    if (userById != null) {
                        userById.setChecked(userPhotosTbl.getChecked());
                        userDao.saveUser(userById);
                    } else {
                        logger.info("manggo无此用户数据,用户id为:" + userPhotosTbl.getUserId());
                    }

                }

                return true;

            } else {
                return false;
            }

        }

    }

}
